/*
 * 
 */
package com.yuanmei.stockeyes.charts;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnTouchListener;

import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;
import com.yuanmei.stockeyes.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.limc.androidcharts.entity.DateValueEntity;
import cn.limc.androidcharts.entity.LineEntity;
import cn.limc.androidcharts.event.IDisplayCursorListener;
import cn.limc.androidcharts.view.SlipMinusLineChart;

public class TalibAroonChart implements ITalibChart {
    private final Core mTaLibCore = new Core();
    private Context mContext;
    private SlipMinusLineChart mAroonChartView;
    private static final String TAG = TalibAroonChart.class.getName();

    private List<DateValueEntity> mAroonDownDateValueList;
    private List<DateValueEntity> mAroonUpDateValueList;
    private LineEntity<DateValueEntity> mAroonDownLineEntity;
    private LineEntity<DateValueEntity> mAroonUpLineEntity;

    private int    mAroonPeriod = 3;
    private String mAroonPeriodName;

    private int mTaIdxDataSetsBegin;
    private int mTaIdxDataSetsNumber;

    public TalibAroonChart(Context context) {
        mAroonChartView = new SlipMinusLineChart(context);
        TalibChartFactory.commonInitGridChart(mAroonChartView);
        TalibChartFactory.commonInitSlipLineChart(mAroonChartView);

        mContext          =  mAroonChartView.getContext();
        mAroonPeriodName = mContext.getString(R.string.param_aroon_period);
    }

    @Override
    public List<Map<String, Object>> getTaParamsList() {
        List<Map<String, Object>> outParamsList = new ArrayList<Map<String, Object>>();

        Map<String, Object> item = new HashMap<String, Object>();
        item.put(ITalibChart.ITaParamNameKey,  mAroonPeriodName);
        item.put(ITalibChart.ITaParamValueKey, mAroonPeriod);
        outParamsList.add(item);

        return outParamsList;
    }

    @Override
    public void setTaParams(Hashtable<String, Integer> inParamMaps) {
        if (inParamMaps.containsKey(mAroonPeriodName)
                ) {
            mAroonPeriod = inParamMaps.get(mAroonPeriodName);
        }
    }

    @Override
    public boolean reloadData(int dataLen,
                              float[] openPrices,
                              float[] highPrices,
                              float[] lowPrices,
                              float[] closePrices,
                              float[] volumes,
                              float[] amounts,
                              int[] dates) {
        MInteger outBegIdx    = new MInteger();
        MInteger outNBElement = new MInteger();
        double[] outAroonDown = new double[dataLen];
        double[] outAroonUp   = new double[dataLen];

        /*
        public RetCode aroon( int startIdx,
        int endIdx,
        double inHigh[],
        double inLow[],
        int optInTimePeriod,
        MInteger outBegIdx,
        MInteger outNBElement,
        double outAroonDown[],
        double outAroonUp[] )
        */


        RetCode retCode = mTaLibCore.aroon(0, closePrices.length - 1,
                highPrices, lowPrices,
                mAroonPeriod,
                outBegIdx, outNBElement, outAroonDown, outAroonUp);
        if (retCode != RetCode.Success) {
            Log.e(TAG, "AROON Chart calc error retCode=" + retCode);
            return false;
        }

        mAroonDownDateValueList = new ArrayList<>();
        mAroonUpDateValueList   = new ArrayList<>();
        for (int i = 0; i < dataLen; i++) {
            if (i < outBegIdx.value) {
                mAroonDownDateValueList.add(new DateValueEntity(
                        (float) (outAroonDown[0]), dates[i]));
                mAroonUpDateValueList.add(new DateValueEntity(
                        (float) (outAroonUp[0]), dates[i]));
            } else {
                int idx = i - outBegIdx.value;
                mAroonDownDateValueList.add(new DateValueEntity(
                        (float) (outAroonDown[idx]), dates[i]));
                mAroonUpDateValueList.add(new DateValueEntity(
                        (float) (outAroonUp[idx]), dates[i]));
            }
        }

        List<LineEntity<DateValueEntity>> lines = new ArrayList<LineEntity<DateValueEntity>>();

        mAroonDownLineEntity = new LineEntity<DateValueEntity>();
        mAroonDownLineEntity.setTitle("DOWN");
        mAroonDownLineEntity.setLineColor(Color.GREEN);
        mAroonDownLineEntity.setLineData(mAroonDownDateValueList);
        lines.add(mAroonDownLineEntity);

        mAroonUpLineEntity = new LineEntity<DateValueEntity>();
        mAroonUpLineEntity.setTitle("UP");
        mAroonUpLineEntity.setLineColor(Color.RED);
        mAroonUpLineEntity.setLineData(mAroonUpDateValueList);
        lines.add(mAroonUpLineEntity);

        mAroonChartView.setLinesData(lines);

        mTaIdxDataSetsBegin  = outBegIdx.value;
        mTaIdxDataSetsNumber = outNBElement.value;
        return true;
    }

    @Override
    public int getTaIndexNumber() {
        return mTaIdxDataSetsNumber;
    }

    @Override
    public String getTaIndexParms() {
        return   mAroonPeriod + "";
    }

    @Override
    public String getTaIndexShortName() {
        return "AROON";
    }

    @Override
    public Spanned getTaIndexDetail() {
        if (null == mAroonDownDateValueList ||
                null == mAroonUpDateValueList) {
            return new SpannableString("----");
        }
        int index = mAroonChartView.getSelectedIndex();

        DateValueEntity downEntity = mAroonDownDateValueList.get(index);
        String          downText   = String.format(Locale.ENGLISH, "Down:%.2f ",
                downEntity.getValue());

        DateValueEntity upEntity = mAroonUpDateValueList.get(index);
        String          upText   = String.format(Locale.ENGLISH, "Up:%.2f ",
                downEntity.getValue());

        String    finalString = downText + upText;
        Spannable sb          = new SpannableString(finalString);

        sb.setSpan(new ForegroundColorSpan(mAroonDownLineEntity.getLineColor()), finalString.indexOf(downText),
                finalString.indexOf(downText) + downText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

        sb.setSpan(new ForegroundColorSpan(mAroonUpLineEntity.getLineColor()), finalString.indexOf(downText),
                finalString.indexOf(upText) + upText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);


        return (sb);
    }

    @Override
    public int getTaIndexBegin() {
        return mTaIdxDataSetsBegin;
    }

    @Override
    public View getChartView() {
        return mAroonChartView;
    }

    @Override
    public void setDisplayFrom(int from) {
        mAroonChartView.setDisplayFrom(from);
    }

    @Override
    public void setDisplayCrossXOnTouch(boolean displayCrossXOnTouch) {
        mAroonChartView.setDisplayCrossXOnTouch(displayCrossXOnTouch);
    }

    @Override
    public void setDisplayNumber(int number) {
        mAroonChartView.setDisplayNumber(number);
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        mAroonChartView.setOnTouchListener(l);
    }

    @Override
    public void setOnDisplayCursorListener(
            IDisplayCursorListener l) {
        mAroonChartView.setOnDisplayCursorListener(l);
    }

    @Override
    public PointF getTouchPoint() {
        return mAroonChartView.getTouchPoint();
    }

    @Override
    public void setTouchPoint(PointF touchPoint) {
        mAroonChartView.setTouchPoint(touchPoint);
    }

    @Override
    public void postInvalidate() {
        mAroonChartView.postInvalidate();
    }

    @Override
    public float getValidMiddleY() {
        return mAroonChartView.getValidMiddleY();
    }
}
